#ifndef MESH_H
#define MESH_H

#include "Game Engine/Header/d3dUtil.h"
#include "Game Engine/Header/Light.h"
#include <vector>
#include <string>
 
struct MeshVertex
{
	D3DXVECTOR3 pos;
	D3DXVECTOR3 tangent;
	D3DXVECTOR3 normal;
	D3DXVECTOR2 texC;
};

struct MeshFaces
{
	UINT i0;
	UINT i1;
	UINT i2;
};

class Mesh
{
public:
	Mesh();
	~Mesh();

	ID3DX10Mesh* d3dxMesh();

	void Init( ID3D10Device* device, std::wstring filename, ID3D10Effect* mFX, bool useShadows );

	void GetAABB(D3DXVECTOR3& vMin, D3DXVECTOR3& vMax);

	void SetLight(const Light& light);
	void SetEyePos(const D3DXVECTOR3& eyePos);
	void SetCubeMap(ID3D10ShaderResourceView* cubeMap);
	void SetShadowMap(ID3D10ShaderResourceView* shadowMap);
	void EnableCubeMap(bool enable);
	void UseTangent( bool tangent );
	void UnbindShadowMap();

	void Draw(const D3DXMATRIX& world, const D3DXMATRIX& lightViewProj);
	void DrawToShadowMap(
		ID3D10EffectShaderResourceVariable* diffuseMapVar,
		ID3D10EffectPass* pass);

	ID3DX10Mesh* GetMeshData();
	std::vector<ID3D10ShaderResourceView*> GetDiffuseTextures();

	MeshVertex*	GetVertices();
	int*		GetIndices();

	UINT		GetNumSubsets();
	int			GetNumVertices();
	int			GetNumTriangles();

private:
	Mesh(const Mesh& rhs);
	Mesh& operator=(const Mesh& rhs);

	void GetFXHandles( ID3D10Effect* mFX );

private:
	bool mUseShadows;

	MeshVertex* mVerts;
	int* mIndices;
	ID3D10Device* md3dDevice;
	ID3DX10Mesh* mMeshData;

	ID3D10EffectTechnique* mTech;
	ID3D10EffectVariable* mfxLightVar;
	ID3D10EffectVariable* mfxEyePosVar;
	ID3D10EffectMatrixVariable* mfxLightWVPVar;
	ID3D10EffectMatrixVariable* mfxWVPVar;
	ID3D10EffectMatrixVariable* mfxWorldVar;
	ID3D10EffectVectorVariable* mfxReflectMtrlVar;
	ID3D10EffectScalarVariable* mfxCubeMapEnabledVar;
	ID3D10EffectScalarVariable* mfxUseTangentVar;
	ID3D10EffectShaderResourceVariable* mfxDiffuseMapVar;
	ID3D10EffectShaderResourceVariable* mfxSpecMapVar;
	ID3D10EffectShaderResourceVariable* mfxNormalMapVar;
	ID3D10EffectShaderResourceVariable* mfxShadowMapVar;
	ID3D10EffectShaderResourceVariable* mfxCubeMapVar;

	UINT mNumSubsets;
	int mNumVertices;
	int mNumTriangles;
	std::vector<D3DXVECTOR3> mReflectMtrls;
	std::vector<ID3D10ShaderResourceView*> mDiffuseTextures;
	std::vector<ID3D10ShaderResourceView*> mSpecTextures;
	std::vector<ID3D10ShaderResourceView*> mNormalTextures;
};

#endif // MESH